package com.ckzp.core.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * Created by 51594 on 2022/7/27.
 */
public class StringUtil {


    /**
     * @param source
     * @param delim
     * @return
     */
    public static String[] split(String source, String delim) {
        String[] wordLists;
        if (source == null) {
            wordLists = new String[1];
            wordLists[0] = source;
            return wordLists;
        }
        if (delim == null) delim = ",";
        StringTokenizer st = new StringTokenizer(source, delim);
        int total = st.countTokens();
        wordLists = new String[total];
        for (int i = 0; i < total; i++) {
            wordLists[i] = st.nextToken();
        }
        return wordLists;
    }

    public static List splitList(String str, String delim) {
        List splitList = null;
        StringTokenizer st = null;

        if (str == null) {
            return splitList;
        }

        if (delim != null) {
            st = new StringTokenizer(str, delim);
        } else {
            st = new StringTokenizer(str);
        }
        if (st != null && st.hasMoreTokens()) {
            splitList = new ArrayList();

            while (st.hasMoreTokens()) {
                splitList.add(st.nextToken());
            }
        }
        return splitList;
    }

    /**
     * replace全部字府串
     *
     * @param mainString 主字符串
     * @param oldString  需要替换的字符串
     * @param newString  替换的结果
     * @return
     */
    public static String replace(String mainString, String oldString, String newString) {
        if (mainString == null) return null;
        if (oldString == null || oldString.length() == 0) return mainString;
        if (newString == null) newString = "";
        int i = mainString.lastIndexOf(oldString);
        if (i < 0) return mainString;
        StringBuffer mainSb = new StringBuffer(mainString);
        while (i >= 0) {
            mainSb.replace(i, i + oldString.length(), newString);
            i = mainString.lastIndexOf(oldString, i - 1);
        }
        return mainSb.toString();
    }

    /**
     * 去掉结束的|，等
     *
     * @param mainString
     * @param splitStr
     * @return
     */
    public static String delEnd(String mainString, String splitStr) {
        if (mainString == null || mainString.length() <= 1) return mainString;
        if (mainString.endsWith(splitStr)) {
            return mainString.substring(0, mainString.length() - 1);
        } else {
            return mainString;
        }
    }


    /**
     * 去掉结束的|，等
     *
     * @param mainString
     * @param splitStr
     * @return
     */
    public static String delBegin(String mainString, String splitStr) {
        if (mainString == null || mainString.length() < 1) return mainString;
        if (mainString.startsWith(splitStr)) {
            return mainString.substring(1);
        } else {
            return mainString;
        }
    }


    /**
     * 去掉结束的|，等
     *
     * @param mainString
     * @param splitStr
     * @return
     */
    public static String delBeginEnd(String mainString, String splitStr) {
        mainString = delBegin(mainString, splitStr);
        mainString = delEnd(mainString, splitStr);
        return mainString;
    }

    /**
     * @param str
     * @param defaultValue
     * @return
     */
    public static double getDouble(String str, double defaultValue) {
        double returnInt = defaultValue;
        try {
            returnInt = Double.parseDouble(str);
        } catch (Exception e) {
            returnInt = defaultValue;
        }
        return returnInt;
    }

    /**
     * @param str
     * @param defaultValue
     * @return
     */
    public static int getInt(String str, int defaultValue) {
        int returnInt = defaultValue;
        try {
            returnInt = Integer.parseInt(str);
        } catch (Exception e) {
            returnInt = defaultValue;
        }
        return returnInt;
    }


    public static int getInt(Map m, String para) {
        return getInt(getString(m.get(para), ""), 0);
    }


    public static long getLong(String str, long defaultValue) {
        long returnInt = defaultValue;
        try {
            returnInt = Long.parseLong(str);
        } catch (Exception e) {
            returnInt = defaultValue;
        }
        return returnInt;
    }


    public static long getLong(Map m, String para) {
        return getLong(getString(m.get(para), ""), 0);
    }


    public static String getString(Object str, String defValue) {
        return getString(String.valueOf(str), defValue);
    }

    /**
     * 从MAP中取值
     *
     * @param m
     * @param key
     * @return
     */
    public static String getString(Map m, String key) {
        return getString(m.get(key));
    }


    public static String getString(String[] s, String s1) {
        String str = "";
        if (s == null) return str;
        for (int k = 0; k < s.length; k++) {
            str += s[k];
            if (k < s.length - 1) str += s1;
        }
        return str;
    }

    public static String getString(Object str) {
        return getString(String.valueOf(str), "");
    }


    public static String getString(String str, String defValue) {
        return str == null ? defValue : str;
    }

    public static boolean isEndWith(String sourceString, String keyString) {
        int index = sourceString.lastIndexOf(keyString);
        if (index == sourceString.length() - keyString.length())
            return true;
        else
            return false;
    }


    public static boolean isEmpty(String str) {
        if (str == null || str.length() == 0) {
            return true;
        }
        return false;
    }

}
